CODESUBDIR := $(COMPONENT_PATH)/../../../../libAtomVM

LIBRARY := -llibAtomVM

CFLAGS := -Og -ggdb -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -DWITH_POSIX -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H  -g -pipe -fvisibility=hidden -fPIC
CPPFLAGS := -Og -ggdb -ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib -DWITH_POSIX -DMBEDTLS_CONFIG_FILE='"mbedtls/esp_config.h"' -DHAVE_CONFIG_H  -g -pipe -fvisibility=hidden -fPIC

INCLUDE_FIXUP_DIR := $(IDF_PATH)/components/lwip/include/lwip/posix

COMPONENT_ADD_INCLUDEDIRS := ../../../../libAtomVM

COMPONENT_ADD_LDFLAGS := $(LIBRARY)

CINCLUDES := $(COMPONENT_INCLUDES) $(COMPONENT_EXTRA_INCLUDES) $(COMPONENT_ADD_INCLUDEDIRS) $(INCLUDE_FIXUP_DIR)

CMAKE_TOOLCHAIN_FILE := cmake_toolchain_file.txt

$(CMAKE_TOOLCHAIN_FILE):
	${file >$(CMAKE_TOOLCHAIN_FILE),#Autogenerated from component.mk}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_SYSTEM_NAME ESP_IDF)}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_SYSTEM_VERSION 3.2)}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_SYSTEM_PROCESSOR xtensa_esp32)}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_C_COMPILER $(CC))}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)}
	${file >>$(CMAKE_TOOLCHAIN_FILE),SET(CFLAGS $(CFLAGS))}
	${foreach incdir,$(CINCLUDES),${file >>$(CMAKE_TOOLCHAIN_FILE),include_directories($(incdir))}}


Makefile: $(CODESUBDIR)/CMakeLists.txt $(CMAKE_TOOLCHAIN_FILE) $(COMPONENT_PATH)/component.mk
	cmake -DCMAKE_TOOLCHAIN_FILE=$(CMAKE_TOOLCHAIN_FILE) $(CODESUBDIR)

build: Makefile
ifeq ($(V),)
	VERBOSE=0 COLOR=1 make
else
	VERBOSE=1 COLOR=1 make
endif

clean: Makefile
	make clean
	rm -rf CMakeCache.txt CMakeFiles *.cmake

COMPONENT_OWNCLEANTARGET := clean
COMPONENT_OWNBUILDTARGE := build
